﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FarseerGames.FarseerPhysics;
using Microsoft.Xna.Framework.Graphics;
using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Collisions;
using FarseerGames.FarseerPhysics.Factories;
using FarseerGames.FarseerPhysics.Controllers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Net;


namespace GrapplingHookGameData
{
    public class Shuriken:PhysicsObject
    {
        #region Fields

        public enum ShurikenState { INACTIVE, FLYING};
        private ShurikenState currentState = ShurikenState.INACTIVE;
        private ShurikenState previousState = ShurikenState.INACTIVE;
        public ShurikenState CurrentState
        {
            get { return currentState; }
            private set { previousState = currentState; currentState = value; }
        }

        private Animation animation;
        private Player player;
        #endregion

        #region Initialization
        public Shuriken(IGameDataCallbacks gameCallback, Vector2 startPosition, Player owner)
            : base(gameCallback, startPosition, 25, 25, "shuriken/SpriteSheet", true)
        {
            this.player = owner;
        }

        public override void LoadContent(ContentManager content, PhysicsSimulator physicsSimulator)
        {
            base.LoadContent(content, physicsSimulator);
            animation = new Animation(spriteSheet, "shuriken", 3, Constants.SHURIKEN_FRAME_ANIMATION_TIME_MS);


            Body = BodyFactory.Instance.CreateCircleBody(physicsSimulator, Width / 2, Constants.SHURIKEN_MASS);
            Geom = GeomFactory.Instance.CreateCircleGeom(physicsSimulator, Body, Width/2, 8);
            Geom.RestitutionCoefficient = 0.25f;

            Geom.OnCollision += new Geom.CollisionEventHandler(gameCallback.onShurikenCollision);
            Geom.Tag = this;

            Body.Position = position;
            Extinguish();
        }
        #endregion


        #region Update

        public override void Update(GameTime gameTime, bool isHost)
        {
            base.Update(gameTime, isHost);

            //clients and host update animations 
            switch (CurrentState)
            {
                case ShurikenState.INACTIVE:
                    break;
                case ShurikenState.FLYING:
                    currentFrame = animation.Update(gameTime);
                    break;
            }
        }


        #endregion

        #region Actions / State Change

        public void Activate()
        {
            CurrentState = ShurikenState.FLYING;
            Body.Enabled = true;
            currentFrame = animation.Start();
        }
        
        public void Extinguish()
        {
            CurrentState = ShurikenState.INACTIVE;
            Body.ResetDynamics();
            Body.Enabled = false;
        }


        #endregion

        #region Draw
        public override void Draw(SpriteBatch spriteBatch)
        {
            if (CurrentState != ShurikenState.INACTIVE)
            {
                base.Draw(spriteBatch);
            }
        }
        #endregion

        #region Networking
        /// <summary>
        /// Writes the data that is needed by clients to draw this shuriken
        /// </summary>
        /// <param name="packetWriter"></param>
        public void WriteDataToClients(PacketWriter packetWriter)
        {
            packetWriter.Write(Position);
            packetWriter.Write((int)CurrentState);
        }

        /// <summary>
        /// Read the data that is needed by clients to draw this grenade
        /// </summary>
        /// <param name="packetWriter"></param>
        public void ReadDataFromHost(PacketReader packetReader)
        {
            //clients set the position directly
            Position = packetReader.ReadVector2();
            CurrentState = (ShurikenState)packetReader.ReadInt32();

        }
        #endregion

    }
}
